using GameToolkit.BehaviourTree;
using System;
using UnityEngine;

namespace GameToolkit.Editor
{
    public class BehaviourInspectorData : ScriptableObject
    {
        //[SerializeField, Tooltip("当节点执行条件不满足时，执行状态更改类型。\n  From Node: 节点自身状态决定\n  Success|Failed: 更改为成功或者失败状态")]
        //internal BehaviourTreeAsset.EInterruptResult m_Interruption;

        //[SerializeField, Tooltip("节点绑定的多个条件的组合逻辑")]
        //internal BehaviourTreeAsset.EConditionLogic m_ConditionLogic;

        [SerializeReference]
        public Composite m_Node;


        [SerializeReference]
        public Composite[] m_Decorators;

        //[SerializeReference]
        //public Composite[] m_Decorators;

        internal void SetTarget(in Composite node)
        {
            //m_ConditionLogic = context.m_ConditionLogic;
            //m_Interruption = context.m_Interruption;
            //m_Node = context.m_Node;
            m_Node = node;
            Composite[] decors;
            if(node is ExecutableComposite ec)
            {
                decors = ec.m_Decorations;
            }
            else
            {
                decors = new Composite[] { node };
            }
            m_Decorators = new Composite[decors == null ? 0 : decors.Length];
            if (m_Decorators.Length > 0)
                Array.Copy(decors, m_Decorators, decors.Length);
            //if(node is ControllerComposite cnode)
            //{
            //    m_Decorators = new Composite[cnode.m_Decorators == null ? 0 : cnode.m_Decorators.Length];
            //    if (m_Decorators.Length > 0)
            //        Array.Copy(cnode.m_Decorators, m_Decorators, m_Decorators.Length);
            //}
            //else if(node is ActionComposite anode)
            //{
            //    m_Decorators = new Composite[anode.m_Decorators == null ? 0 : anode.m_Decorators.Length];
            //    if (m_Decorators.Length > 0)
            //        Array.Copy(anode.m_Decorators, m_Decorators, m_Decorators.Length);
            //}
            //else
            //{
            //    m_Decorators = new Composite[0];
            //}
        }

        internal void ApplyModifyValues(ref Composite node)
        {
            var decors = new Composite[m_Decorators == null ? 0 : m_Decorators.Length];
            if (decors.Length > 0)
                Array.Copy(m_Decorators, decors, decors.Length);
            if (node is ExecutableComposite ec)
                ec.m_Decorations = decors;
            //context.m_Interruption = m_Interruption;
            //context.m_Node = m_Node;
            //context.m_ConditionLogic = m_ConditionLogic;
            //var len = m_Decorators == null ? 0 : m_Decorators.Length;
            //if (context.m_Decorations == null || context.m_Decorations.Length != len)
            //{
            //    context.m_Decorations = new DecoratorComposite[len];
            //}
            //if (len > 0)
            //{
            //    Array.Copy(m_Decorators, context.m_Decorations, m_Decorators.Length);
            //}
        }
    }

}
